//
//  ViewController.m
//  06-nsurlsession03-data的代理方法
//
//  Created by J on 16/2/26.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLSessionTaskDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSURLSession* session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
    
    NSMutableURLRequest* mRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4"]];
   
    NSURLSessionDataTask* task =   [session dataTaskWithRequest:mRequest];
    [task resume];
    
//    [task cancel];//取消 NSURLSession中
//    - (void)suspend;//暂停
//    - (void)resume;恢复
}


#pragma mark --NSURLSessionTaskDelegate


- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler{
    
    completionHandler(NSURLSessionResponseAllow);//允许处理服务器响应 才回继续接受服务器数据
    
    NSLog(@"%s",__func__);

    
}
//接受到服务器数据(可能会被调用多次)
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
    didReceiveData:(NSData *)data{
    
    NSLog(@"%s",__func__);
    
}


//请求成功或者失败(如果失败,error);
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
    NSLog(@"%s",__func__);
}


@end
